package master.photo.controller


import master.photo.domain.Photo;
import master.photo.service.GroovyImageResizer 
import org.springframework.beans.factory.annotation.Autowired 


class PhotoController {
	
	@Autowired
	private GroovyImageResizer resizer;
	
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	def index = {
		redirect(action: "list", params: params)
	}
	
	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[photoInstanceList: Photo.list(params), photoInstanceTotal: Photo.count()]
	}
	
	def create = {
		def photoInstance = new Photo()
		photoInstance.properties = params
		return [photoInstance: photoInstance]
	}
	
	def save = {
		def photoInstance = new Photo(params)
		photoInstance.photo = resizer.scaleJPG(photoInstance.photo, 400, 400)
		if (photoInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'photo.label', default: 'Photo'), photoInstance.id])}"
			redirect(action: "show", id: photoInstance.id)
		}
		else {
			render(view: "create", model: [photoInstance: photoInstance])
		}
	}
	
	def show = {
		def photoInstance = Photo.get(params.id)
		if (!photoInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photo.label', default: 'Photo'), params.id])}"
			redirect(action: "list")
		}
		else {
			[photoInstance: photoInstance]
		}
	}
	
	def edit = {
		def photoInstance = Photo.get(params.id)
		if (!photoInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photo.label', default: 'Photo'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [photoInstance: photoInstance]
		}
	}
	
	def update = {
		def photoInstance = Photo.get(params.id)
		if (photoInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (photoInstance.version > version) {
					
					photoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'photo.label', default: 'Photo')] as Object[], "Another user has updated this Photo while you were editing")
					render(view: "edit", model: [photoInstance: photoInstance])
					return
				}
			}
			photoInstance.properties = params
			if (!photoInstance.hasErrors() && photoInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'photo.label', default: 'Photo'), photoInstance.id])}"
				redirect(action: "show", id: photoInstance.id)
			}
			else {
				render(view: "edit", model: [photoInstance: photoInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photo.label', default: 'Photo'), params.id])}"
			redirect(action: "list")
		}
	}
	
	def delete = {
		def photoInstance = Photo.get(params.id)
		if (photoInstance) {
			try {
				photoInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'photo.label', default: 'Photo'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'photo.label', default: 'Photo'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photo.label', default: 'Photo'), params.id])}"
			redirect(action: "list")
		}
	}
	
	def slide(direction, currentPhotoId) {
		def newIndex = 0;
		def photos = Photo.list(sort: "dateCreated", order: "asc")
		
		for (def i = 0; i < photos.size; i++) {
			if (currentPhotoId as Long == photos.get(i).id) {
				newIndex = i + direction;
				break;
			}
		}
		
		if (newIndex < 0) {
			newIndex = photos.size + newIndex;
		} else if (newIndex >= photos.size) {
			newIndex = 0;
		}
		
		redirect(action: "show", params: [id: photos.get(newIndex).id])
	}
	
	def slideDown = {
		slide(-1, params.id)
	}
	
	def slideUp = {
		slide(1, params.id)
	}
	
}
